//
//  TTChatCell.swift
//  TTProduct
//
//  Created by Toj on 3/30/21.
//  Copyright © 2021 TTProduct. All rights reserved.
//

import Cocoa

class TTChatCell: NSTableRowView {
    
    var chatMessage: TTChatMessage? {
        didSet {
            guard let chat = chatMessage else { return }
            let alignment: NSTextAlignment = (chat.role == .remote) ? .left : .right
            nicknameLabel.alignment = alignment
            massageLabel.alignment = alignment
        }
    }
    
    lazy var nicknameLabel: TTLabel = {
        let view = TTLabel()
        view.font = NSFont.systemFont(ofSize: 9)
        view.textColor = NSColor.init(hexColor: "#AAAAAA")
        return view
    }()
    
    lazy var emojiImageView: NSImageView = {
        let view = NSImageView()
        return view
    }()
    
    lazy var massageImageView: NSImageView = {
        let view = NSImageView()
        view.imageScaling = .scaleAxesIndependently
        return view
    }()
    
    lazy var massageLabel: TTLabel = {
        let view = TTLabel()
        view.lineBreakMode = .byCharWrapping
        view.font = NSFont.systemFont(ofSize: 11)
        view.textColor = NSColor.black
        return view
    }()
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layout() {
        super.layout()
        
        setupMakeLayoutSubviews()
    }
    
    func setupMakeLayoutSubviews() {
        nicknameLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(self)
            make.left.equalTo(10)
            make.right.equalTo(-10)
        }
    }
    
    deinit {
        print(#function + className)
    }
}

// MARK: - setup
extension TTChatCell {
       
   private func setupMakeAddSubviews() {
        addSubview(nicknameLabel)
        addSubview(emojiImageView)
        addSubview(massageImageView)
        addSubview(massageLabel)
    }
}
